package com.feishi.project.one;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2018-10-23
 * Time: 下午3:54
 */
public class Main {
    public static Status mode;
    public static boolean modeChange;
    public static boolean newData=true;
    private static Value value;
    private static Out out;
    private static mode1 mode1;
    private static int count;


    public static void main(String[] args) throws InterruptedException {
      new OutData().start();
        while (true){
            Thread.sleep(2);

            count++;
            if(newData){
                //set mode
            }
            switchToNewMode();
            modeExecuter();

        }
    }

    private static void modeExecuter() {
        switch (mode.mode) {
            case 1:
                //模式1操作
                break;
            case 2:
                //动态恒阻
                mode1();
                break;
            case 3:
                out.开关1 = false;
                out.开关4 = true;
                break;
            case 4:
                out.开关5 = false;
                out.开关2 = true;
                break;
            case 5:
                out.开关3 = false;
                out.开关4 = true;
        }
    }

    private static int mode1Count;
    private static void mode1() {
        mode1Count++;
        int target = (int) (mode1.阀值变化步长 * 1000 / 22111);
        if(mode1Count%target==0){
            out.输出脉冲=true;
        }
        int 采集计数器=3929;
        if(mode1Count%target==0){
            resetMode1();
        }
    }


    private static void resetMode1() {
        double 旧阻力 = mode1.oldValue.get阻力();
        double 新阻力 = value.get阻力();
        double 目标阻力= mode1.get目标阻力();
        if(新阻力/目标阻力<1.1&&新阻力/目标阻力>0.9){
            return;//无需调节
        }
        //阀门过调,纠正
        if((目标阻力-新阻力)*(目标阻力-旧阻力)<0){
            mode1.阀值变化步长=-mode1.阀值变化步长/ mode1.阀值变化系数;
            return;
        }else{

            /**
             * 速度系数需要单独考虑
             * */
            //阀门方向与
            if(mode1.阀值变化步长*(目标阻力-新阻力)<0){
                mode1.阀值变化步长=-mode1.阀值变化步长/ mode1.阀值变化系数;
            }

            /**
             * 速率调整
             * */
            if(Math.abs(目标阻力-新阻力)>Math.abs(目标阻力-旧阻力)){
                mode1.阀值变化步长= mode1.阀值变化步长* mode1.阀值变化系数;
            }else{
                mode1.阀值变化步长= mode1.阀值变化步长/mode1.阀值变化系数;
            }
        }
        out.阀芯开口比+= mode1.阀值变化步长;
        mode1.oldValue=value;
        mode1.oldCount=count;
        mode1.level=mode.level;
    }


    private static void switchToNewMode() {
        if(modeChange){
            switch (mode.mode){
                case 1:
                    out.开关1=false;
                    out.开关2=true;
                    break;
                case 2:
                    out.开关3=false;
                    out.开关2=true;
                    mode1 =new mode1();
                    mode1.oldValue=value;
                    mode1.oldCount=count;
                    mode1.level=mode.level;
                    resetMode1();
                    break;
                case 3:
                    out.开关1=false;
                    out.开关4=true;
                    break;
                case 4:
                    out.开关5=false;
                    out.开关2=true;
                    break;
                case 5:
                    out.开关3=false;
                    out.开关4=true;
                    break;
            }
            modeChange=false;
        }
    }

    private static Value getValues() {
        return value;
    }

}
